(in-package #:lilu-browser)

;;;
;;; simple browser program
;;;

(defclass simple-browser-application-mixin (lilu-app:simple-program-application-mixin)
  ()
  (:default-initargs
   :tags '(:browser)
   :make-args-fn #'(lambda (app &key url)
                     (declare (ignore app))
                     (when url (list url)))))

(defclass simple-browser-program-application (simple-browser-application-mixin
                                              lilu-app:application)
  ())

(defmethod run-open-browser ((app simple-browser-program-application) &key url error-p wait-p)
  (lilu-app:run-simple-program app :url url :error-p error-p :wait-p wait-p))

(defclass simple-browser-terminal-application (simple-browser-application-mixin
                                               lilu-app:application)
  ())

(defmethod run-open-browser ((app simple-browser-terminal-application) &key url error-p wait-p)
  (lilu-terminal:open-terminal :cmd (lilu-app:make-simple-program-shell-command app :url url)
                               :error-p error-p
                               :wait-p wait-p))

(defun register-simple-browser (string-designator program
                         version-option version-script
                         require-terminal-p)
  (lilu-app:register-application
   (make-instance (if require-terminal-p
                      'simple-browser-terminal-application
                      'simple-browser-program-application)
                  :name (string string-designator)
                  :program program
                  :version-option version-option
                  :version-script version-script)))

;;;
;;; registered browsers
;;;

(register-simple-browser :chromium "chromium" "--version"
                         "chromium --version | awk '{print $2}'"
                         nil)

(register-simple-browser :firefox "firefox" "--version"
                         "firefox --version | awk '{print $3}'"
                         nil)

(register-simple-browser :firefox-dev "firefox-developer-edition" "--version"
                         "firefox-developer-edition --version | awk '{print $3}' | tr \"b\" \" \" |  awk '{print $1}'"
                         nil)

(register-simple-browser :midori "midori" "--version"
                         "midori --version | head -1 | awk '{print $2}'"
                         nil)

(register-simple-browser :falkon "falkon" "--version"
                         "falkon --version | head -1 | awk '{print $2}'"
                         nil)

(register-simple-browser :opera "opera" "--version"
                         "opera --version"
                         nil)

(register-simple-browser :palemoon "palemoon" "--version"
                         "palemoon --version | head -1 | awk '{ print $5  }'"
                         nil)

(register-simple-browser :links "links" "-version"
                         "links -version | awk '{ print $2 }'"
                         t)

(register-simple-browser :elinks "elinks" "-version"
                         "elinks -version | head -1 | awk '{ print $2 }'"
                         t)

(register-simple-browser :w3m "w3m" "-version"
                         "w3m -version | tr \"/+\" \" \" | awk '{ print $4}'"
                         t)

(setf *browser* (some #'(lambda (app)
                          (when (lilu-app:application-installed-p app)
                            app))
                      (reverse (lilu-app:find-applications :browser))))
